public class MirrorTree {
    /**
     * 请完成一个函数，输入一个二叉树，该函数输出它的镜像。
     * @param root
     * @return
     */
    public TreeNode mirrorTree(TreeNode root) {
        if (root==null)
            return root;

         swapNode(root);
         return root;
    }

    public void swapNode(TreeNode treeNode){
        if (treeNode==null)
            return;
        if (treeNode.left==null&&treeNode.right==null){
            return;
        }else if (treeNode.left==null){
             treeNode.left = treeNode.right;
             treeNode.right = null;
             swapNode(treeNode.left);
        }else if(treeNode.right==null){
              treeNode.right = treeNode.left;
              treeNode.left = null;
              swapNode(treeNode.right);
        }else {
            TreeNode tmp = treeNode.left;
            treeNode.left = treeNode.right;
            treeNode.right = tmp;
            swapNode(treeNode.right);
            swapNode(treeNode.left);

        }
    }
}
